package com.woniuxy.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.admin.entity.PawnShop;
import com.woniuxy.admin.exception.AdminException;
import com.woniuxy.admin.dto.AdminAccountPageDto;
import com.woniuxy.admin.entity.AdminAccount;
import com.woniuxy.admin.entity.AdminInfo;
import com.woniuxy.admin.mapper.AdminAccountMapper;
import com.woniuxy.admin.mapper.AdminInfoMapper;
import com.woniuxy.admin.mapper.PawnShopMapper;
import com.woniuxy.admin.service.AdminAccountService;
import com.woniuxy.admin.service.AdminInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.admin.service.AdminRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ...
 * @since 2024年05月22日
 */
@Service
public class AdminInfoServiceImpl extends ServiceImpl<AdminInfoMapper, AdminInfo> implements AdminInfoService {


    @Resource
    private AdminAccountMapper adminAccountMapper;
    @Resource
    private AdminRoleService adminRoleService;
    @Resource
    private PawnShopMapper pawnShopMapper;
    @Resource
    private AdminAccountService adminAccountService;

    /**
     * 修改管理员信息
     * @param nickName 昵称
     */
    @Override
    public void updateAdminInfo(Integer accountId,String nickName) {
        AdminAccount adminAccount = adminAccountMapper.selectById(accountId);
        if(adminAccount==null){
            throw new AdminException(20001,"管理员不存在");
        }
        QueryWrapper<AdminInfo>wrapper=new QueryWrapper<>();
        wrapper.eq("account_id",accountId);
        AdminInfo adminInfo = this.getOne(wrapper);
        adminInfo.setNickName(nickName);
        this.updateById(adminInfo);
    }

    /**
     * 修改管理员头像
     * @param accountId 账号Id
     * @param filePath  头像路径
     */
    @Override
    public void updateAdminHeader(Integer accountId, String filePath) {
        AdminInfo adminInfo = this.getOne(new QueryWrapper<AdminInfo>().eq("account_id", accountId));
        adminInfo.setAdminHeader(filePath);
        this.updateById(adminInfo);
    }

    /**
     * 根据ID查询管理员
     * @param accountId 管理员id
     * @return
     */
    @Override
    public AdminAccountPageDto findAdminInfoByAccountId(Integer accountId) {
        AdminAccount adminAccount = adminAccountMapper.selectById(accountId);
        if(adminAccount==null) throw new AdminException(20001,"管理员不存在");
        AdminInfo adminInfo = this.getOne(new QueryWrapper<AdminInfo>().eq("account_id", accountId));
        AdminAccountPageDto dto = BeanUtil.toBean(adminInfo, AdminAccountPageDto.class);
        dto.setAdminName(adminAccount.getAdminName());
        dto.setAdminPhone(adminAccount.getAdminPhone());
        dto.setState(adminAccount.getState());
        String s = adminRoleService.loadAllRoleNameByAccountId(accountId);
        Integer roleId= adminRoleService.selectRoleId(accountId);
        dto.setRoleId(roleId);
        dto.setRole(s);

        return dto;
    }

    /**
     * 查询未绑定门店店长
     *
     * @return
     */
    @Override
    public List<AdminAccountPageDto> selectAllShopBoss() {
        List<AdminAccountPageDto>list=new ArrayList<>();
        List<Integer> accounts = adminAccountService.selectAccountsByRoleId(3);
        List<PawnShop> pawnShops = pawnShopMapper.selectList(new QueryWrapper<PawnShop>());
        if(pawnShops!=null&&pawnShops.size()>0){
            List<Integer> list1 = pawnShops.stream().map(p -> p.getShopBossId()).collect(Collectors.toList());
            accounts.removeAll(list1);
        }
        for (Integer accountId : accounts) {
            AdminAccountPageDto adminInfoByAccountId = this.findAdminInfoByAccountId(accountId);
            list.add(adminInfoByAccountId);
        }

        return list;
    }
}
